<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<p>
    <span>筛选： </span>
    <span>
        <select id="search_year_sel" name="year">
            <s:iterator begin="#request.startYear" end="#request.endYear" var="y">
                <option value="${y}" ${y==year?"selected='selected'":""}>${y}年</option>
            </s:iterator>
        </select>

        <select id="search_month_sel" name="month">
            <s:iterator begin="1" end="12" var="m">
                <option value="${m}" ${m==month?"selected='selected'":""}>${m}月</option>
            </s:iterator>
        </select>
    </span>

    <span id="org_sel_${organizationId}"></span>
    <span>
        <select id="duty_sel" style="opacity: 0;">
            <option value="-1">所有职务</option>
        </select>
    </span>

    <%--<br/><br/><span style="padding-left: 28px;">&nbsp;</span>--%>
    <select name="form-timezone2" id="edu_sel" style="opacity: 0;">
        <option value="-1">所有学历</option>
        <option value="2">初中</option>
        <option value="3">高中</option>
        <option value="4">专科</option>
        <option value="5">本科</option>
        <option value="6">硕士</option>
        <option value="7">博士</option>
    </select>

    <select name="form-timezone2" id="sex_sel" style="opacity: 0;">
        <option value="-1">所有性别</option>
        <option value="0">男</option>
        <option value="1">女</option>
    </select>

    <select id="leave_reason_sel">
        <s:iterator value="{'所有离职原因','薪资低','公司福利差','工作环境不如意','不满公司的政策和措施', '没有事业发展机会', '缺少培训和机会', '工作量太少', '工作太枯燥', '同事关系不融洽',
            '与上司关系不和', '找到更好的工作', '自己经营生意', '家庭原因', '个人感情原因', '求学深造', '身体健康原因', '转换行业', '考上公务员', '住所变迁', '其他'}" var='reason'>
            <option>${reason}</option>
        </s:iterator>
    </select>

    <input type="button" value="点击搜索" onclick="javascript:onSearchClick()">
</p>

<script type="text/javascript">
    $(function () {
        setTimeout('loadChildrenOrgsByTab(${organizationId})', 600);
    })

    /**
     * Tab Change that load the child org
     * @param orgId
     */
    function loadChildrenOrgsByTab(orgId) {
        $("input[type='checkbox']").each(function () {
            $(this).attr("checked", false);
        });
        if ($("#all_select") != null) {
            $("#all_select").parent().attr("class", "");
        }
        //load the organization
        $.ajax({
            type:"get",
            url:"/ajax/organization-children.xhtml?parentId=" + orgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "<div id=\"uniform-undefined-" + orgId + "\" class=\"selector\">";
                strHTML += "<span style=\"-moz-user-select: none;\">所有部门</span>";
                strHTML += "<select style='opacity: 0;' onchange='loadChildrenOrgs(this," + orgId + ")'>";
                strHTML += "<option value=\"-1\">所有部门</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, org) {
                        strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                    });
                }
                strHTML += "</select></div>";
                $("#org_sel_" + orgId).html("");
                $("#org_sel_" + orgId).append(strHTML);
            }
        });
        // load the duty
        loadDutyByOrgId(orgId);
    }

    function loadChildrenOrgs(evtObj, parentId) {
        var evtObjId = evtObj.options[evtObj.selectedIndex].value;
        $(evtObj).prev().html($(evtObj).find("option:selected").text());
        $("#uniform-undefined-" + parentId).nextAll().remove();
        $.ajax({
            type:"get",
            url:"/ajax/organization-children.xhtml?parentId=" + evtObjId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                if (data != null && data.length > 0) {
                    var strHTML = "<div id=\"uniform-undefined-" + evtObjId + "\" class=\"selector\">";
                    strHTML += "<span style=\"-moz-user-select: none;\">所有部门</span>";
                    strHTML += "<select style='opacity: 0;' onchange='loadChildrenOrgs(this," + evtObjId + ")'>";
                    strHTML += "<option value=\"-1\">所有部门</option>";
                    $.each(data, function (i, org) {
                        strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                    });
                    strHTML += "</select></div>";
                    $("#uniform-undefined-" + parentId).after(strHTML);
                }
            }
        });
        //load duty list
        loadDutyByOrgId(evtObjId);
    }

    function loadDutyByOrgId(orgId) {
        //load the duty
        $.ajax({
            type:"get",
            url:"/ajax/duty-list.xhtml?organizationId=" + orgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "<option value=\"-1\">所有职务</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, duty) {
                        strHTML += "<option value=\"" + duty.id + "\">" + duty.dutyName + "</option>";
                    });
                }
                $("#duty_sel").prev("span").html("所有职务");
                $("#duty_sel").html("");
                $("#duty_sel").append(strHTML);
            }
        });
    }

    function onSearchClick(){
        var year = $("#search_year_sel").val();
        var month = $("#search_month_sel").val();
        var orgs = $("#org_sel_${organizationId}").children("div");
        var orgId = -1;
        if(orgs != null && orgs.length > 0){
            var last = orgs[orgs.length - 1];
            if($(last).find("span").html() == "所有部门"){
                var divId = $(last).attr("id"); //uniform-undefined-4
                orgId = divId.substr(divId.lastIndexOf("-")+1, divId.length);
            }else{
                orgId = $(last).find("select").val();
            }
        }
        var dutyId = $("#duty_sel").val();
        var edu = $("#edu_sel").val();
        var sex = $("#sex_sel").val();
        var reason = $("#leave_reason_sel").val();
        if(reason == "所有离职原因"){
            reason = "";
        }
        var data = {"year":year, "month":month, "orgId":orgId, "dutyId":dutyId, "edu":edu, "sex":sex, "reason":reason, "organizationId":"${organizationId}"};
        doSearch(data);
    }
</script>